In a multi-threaded processing environment, two or more threads may require access to a common resource, for example, to modify a variable stored in shared memory, etc. Uncoordinated access to shared resources by multiple threads may result in more than one thread independently, and generally simultaneously modifying the data. The result of independent and simultaneous modification may be that the final data does not properly reflect each of the modifications, or may not reflect the cumulative effect of the modifications.
In software engineering, a lock is a mechanism for coordinating access to a shared resource in a multi-threaded environment. The lock may allow only one thread to access a resource at a given time. Because of the exclusive access, other threads will be prevented from modifying the data while another thread has a lock. The other threads desiring access to the shared resource may have to wait until the thread having the lock has finished with the resource and releases the lock. The sequential access to shared resources provided by locks may ensure that data is only modified by a single thread at any given time. Depending upon the system environment, the various threads in the multi-threaded environment may be running on a single processor, or on multiple processors.